package com.maoxx.stuchoosemssbv_service.controller;

import com.maoxx.stuchoosemssbv_service.entity.Course;
import com.maoxx.stuchoosemssbv_service.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @className: CourseController
 * @author: Maoxx
 * @date: 2024/11/4 13:09
 * @Version: 1.0
 * @description:
 */


@RestController
@RequestMapping("/course")
@CrossOrigin(origins = "*")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/findById/{cid}")
    public List<Course> findById(@PathVariable Integer cid) {
        return courseService.findBySearch(cid);
    }

    @GetMapping("/deleteById/{cid}")
    public boolean deleteById(@PathVariable Integer cid) {
        System.out.println("正在删除课程 cid: " + cid);
        return courseService.deleteById(cid);
    }

    @PutMapping("/updateCourse")
    public boolean updateCourse(@RequestBody Course course){
        return courseService.updateCourse(course);
    }

    @PostMapping("/findBySearch")
    public List<Course> findBySearch(@RequestBody Map<String, String> map) {
        return courseService.findBySearch(map);
    }

    @GetMapping("/getLength")
    public Integer getLength(){
        return courseService.findAll().size();
    }

    @GetMapping("/findByPage/{page}/{size}")
    public List<Course> findPage(@PathVariable Integer page,@PathVariable Integer size){
        return courseService.findPage(page,size);
    }


    @PostMapping("/addCourse")
    public boolean addCourse(@RequestBody Course course){
        return courseService.save(course);
    }


}
